import re

def compute(exp,params,extras={}):
    '''
        计算函数值
        exp: 表达式
        params: 变量参数
        extras: 额外变量
    '''
    pattern= r'[a-zA-Z]\w*'
    newExp=[]
    matchObj = re.search(pattern, exp)
    while matchObj!=None:
        start,end=matchObj.span()
        varname=matchObj.group()
        value=params.get(varname)
        if value==None:
            value=extras.get(varname)
        newExp.append(exp[:start])
        if value!=None:
            newExp.append(str(value))
        else:
            newExp.append(varname)
        exp=exp[end:]
        matchObj = re.search( r'[a-zA-Z]\w*', exp)
    if len(exp)!=0:
        newExp.append(exp)
    newExp="".join(newExp)
    return eval(newExp)
class Function():
    def __init__(self,exp):
        self.exp=exp
    def __call__(self,params,extras={}):
        return compute(self.exp,params,extras)